(ns merpg.utils
    (:import [java.awt.event MouseEvent]))

(def timer
  "The state of the system timer in milliseconds"
  #(System/currentTimeMillis))




(defn map-entry? [obj]
  (instance? clojure.lang.MapEntry obj))

(defn vec-to-map [vec]
	{:pre [(even? (count vec))]}
	(defn vec-splitter [vec]
	  (let [[eka toka & muut] vec]
	  (cons [eka toka] (if (nil? muut)
			       nil
			     (vec-splitter muut)))))
	
	(into {} (vec-splitter vec)))

(defn right-mouse-clicked? [e]
  {:pre [(instance? java.awt.event.MouseEvent e)]}
  (= (.getButton e) MouseEvent/BUTTON3))

(def abs #(java.lang.Math/abs %))

(defn regex-replace [str regex replacement]
  (if (instance? java.util.regex.Pattern regex)
    (recur str (.toString regex) replacement)
    (.replaceAll str regex replacement)))

;(defn to-number
;  "Ei parsi desimaalipilkkua oikein. \"33.4\" => 334"
;  [str]
;  (if (number? str)
;    str
;    (if (empty? str)
;      (throw (Exception. "to-number can't cast empty string to int"))
;      (read-string (regex-replace str #"\D" "")))))

(defn to-number
  [str]
  (read-string (re-find #"[0-9.]*" str)))

;;Used to remove stuff from at least Mapset. Rewrite it to be a seq, and rewrite this with little-schemer algorithms...
(defn vec-remove
  "remove elem in coll"
  [coll pos]
  (vec (concat (subvec coll 0 pos) (subvec coll (inc pos)))))

